﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using H2.NET.Web.Workflow.DwzCode;
using H2.NET.Web.Workflow.Services;
using H2.NET.Common;

namespace H2.NET.Web.Management.Role
{
    public partial class Index : BasePage
    {
        private readonly RoleService _roleService = new RoleService();

        public string name;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (AksRequest.GetQueryString("action") == "delete")
            {
                var id = AksRequest.GetFormInt("id");

                var role = _roleService.GetModel(id);
                if (role.Name == "默认角色")
                {
                    ErrorMsg("对不起，系统默认角色不能删除");
                    return;
                }

                _roleService.Delete(id);

                SuccessMsg("RoleIndex");
                return;
            }

            string strWhere = "1=1";
            if (!string.IsNullOrEmpty(Request.Form["name"]))
            {
                strWhere += " and name like '%" + Request.Form["name"].Trim() + "%'";
            }

            TotalItemCount = _roleService.GetRecordCount(strWhere);
            PageNumber = Convert.ToInt32(Request.Form["pageNum"] ?? "1");

            Pager1.TotalItemCount = TotalItemCount;
            Pager1.PageSize = PageSize;
            Pager1.PageNumber = PageNumber;
            rpt_roles.DataSource = _roleService.GetListByPage(strWhere, "",
                                                              PageSize * (PageNumber - 1) + 1,
                                                              PageSize * PageNumber);
            rpt_roles.DataBind();


            name = Request.Form["name"];
        }
    }
}